package com.recorder.ui.file

import android.content.Intent
import android.media.MediaPlayer
import android.widget.TextView
import android.widget.Toast
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.BaseViewHolder
import com.recorder.R
import java.io.File

/**
 * Created by YWQ on 2018/9/14.
 * 包名:com.recorder
 * 描述:文件列表的适配器
 */
class FileListAdapter(id: Int, data: List<File>) : BaseQuickAdapter<File, BaseViewHolder>(id, data) {
    override fun convert(helper: BaseViewHolder?, item: File?) {
        //mFileName
        helper?.setText(R.id.mFileName, item?.name)
        helper?.setText(R.id.mFilePath, "路径:${item?.path}")



        when {
            item?.name!!.endsWith("wav") -> {
                helper?.setText(R.id.mFileType, "播放")
            }
            item.name!!.endsWith("png") -> {
                helper?.setText(R.id.mFileType, "预览")
            }
            item.name!!.endsWith("JPEG") -> {
                helper?.setText(R.id.mFileType, "预览")
            }
        }

        val mFileType = helper?.getView<TextView>(R.id.mFileType)
        mFileType?.setOnClickListener {


            when {
                item?.name!!.endsWith("wav") -> {

                    if (mFileType.text.toString().equals("播放")) {
                        helper?.setText(R.id.mFileType, "停止")

                        startPaly(item.path)
                    } else {
                        helper?.setText(R.id.mFileType, "播放")
                        stopPlay()
                    }

                }
                item.name.endsWith("png") -> {
                    startImage(item.path)
                }
                item.name.endsWith("JPEG") -> {
                    startImage(item.path)
                }
            }
        }


        mediaPlayer.setOnCompletionListener {
            helper?.setText(R.id.mFileType, "播放")
        }

    }

    private fun stopPlay() {

        mediaPlayer.stop()
    }

  public  val mediaPlayer = MediaPlayer()


    private fun startPaly(path: String?) {
        mediaPlayer.reset()
        mediaPlayer.setDataSource(path)
        mediaPlayer.prepare()
        mediaPlayer.start()
    }

    private fun startImage(path: String?) {

        val intent = Intent(mContext, ImageActivity::class.java)
        intent.putExtra("path", path)
        mContext.startActivity(intent)
    }

}